package com.guojiaming.interview.second.adapter;

import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.guojiaming.interview.R;
import com.guojiaming.interview.second.bean.PlayerInfo;

import java.util.List;
import java.util.Locale;

public class PlayerInfoAdapter extends BaseQuickAdapter<PlayerInfo, BaseViewHolder> {
    public PlayerInfoAdapter(int layoutResId, List<PlayerInfo> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, PlayerInfo playerInfo) {
        if (playerInfo == null) {
            return;
        }
        holder.setText(R.id.tv_player_name, playerInfo.getNickname());
        holder.setText(R.id.tv_layer_team_info, playerInfo.getNickname());
        holder.setText(R.id.tv_id, String.format(Locale.CHINESE, "%d.", playerInfo.getId()));
        ImageView avatar = holder.getView(R.id.iv_avatar);
        Glide.with(avatar).load(playerInfo.getAvatar()).into(avatar);
        List<String> data = playerInfo.convertData();
        PlayerDataAdapter adapter = new PlayerDataAdapter(R.layout.item_player_data_rcv, data);
        RecyclerView rcvData = holder.getView(R.id.rcv_data);
        rcvData.setAdapter(adapter);
        rcvData.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        rcvData.scrollBy(playerInfo.getPosition(),0);
    }

    public void updateContentPosition(int dx) {
        for (PlayerInfo datum : this.getData()) {
            datum.setPosition(dx);
        }
        Log.i("tag", "x=" + dx);
        this.notifyDataSetChanged();
    }
}
